// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu

// #include <cstdio>
// #include <nlohmann/json.hpp>
// #include "coin2/base/time.h"
// #include "coin2/exchange/okex/feed_v3/subsystem.h"
// #include "coin2/exchange/okex_futures/feed_v3/subsystem.h"
// #include "coin2/exchange/base/market/enums.h"

// const char* symbol = "BTC-USD-191227";
// int64_t start = TimestampFromDatetime(2019, 11, 26, 4, 30);
// int64_t end = TimestampFromDatetime(2019, 11, 26, 12, 0);

// using namespace coin::proto;

// class Strategy : public IFeedSubscriber {
//  public:
//   int trade = 0;
//   int book = 0;
//   int discontinuity200 = 0;
//   int discontinuity500 = 0;
//   int discontinuity2000 = 0;
//   int64_t first = 0;
//   int64_t last = 0;
//   std::vector<int64_t> trade_ts;
//   std::vector<int64_t> book_ts;
//   std::vector<double> ask0p;

//   void onTradeFeed(const FeedUpdate& upd) {
//     if(!first) first=upd.trade().timestamp;
//     last=upd.trade().timestamp;

//     trade++;
//     trade_ts.push_back(upd.trade().timestamp);
//   }
//   void onBookFeed(const FeedUpdate& upd) {
//     if (!first) first = upd.book().Timestamp();

//     if (last > 0 && upd.book().Timestamp() - last > 2000'000000)  // > 2000ms ?
//       discontinuity2000++;
//     if (last > 0 && upd.book().Timestamp() - last > 500'000000)
//       discontinuity500++;
//     if (last > 0 && upd.book().Timestamp() - last > 200'000000)
//       discontinuity200++;

//     last = upd.book().Timestamp();
//     book_ts.push_back(upd.book().Timestamp());
//     ask0p.push_back(upd.book().Ask0()->price);
//   }
// };

// Strategy read_coin(const std::string &machine) {
//   Strategy strat;
//   LiveAsyncExecutor ass{LiveSpec{1}};
//   ArchiveSyncExecutor ass{ArchiveConfig::Default(machine, start, end)};
//   FeedSubsystemManager feed(ass, FeedSubsystemType::Archive);
//   feed.SetDefaultStrategy(&strat);
//   feed.SetArchiveConfig(ArchiveConfig::Default(
//       machine,
//       start,
//       end));
//   feed.AddNativeSymbol({Futures, Okex}, symbol);
//   ass.Run();
// };

// template<typename T>
// void run(const std::string& symbol) {
//   auto s1 = read_coin<T>("feed01", symbol);
//   auto s2 = read_coin<T>("feed02", symbol);
//   auto s3 = read_coin<T>("feed05", symbol);

//   printf("feed-01  book: %d\n", s1.book);  // ╮
//   printf("feed-02  book: %d\n", s2.book);  // ╯
//   printf("feed-05  book: %d\n", s3.book);  // ╯

//   printf("feed-01 trade: %d\n", s1.trade);  // ╮
//   printf("feed-02 trade: %d\n", s2.trade);  // ╯
//   printf("feed-05 trade: %d\n", s3.trade);  // ╯

//   printf("feed-01 book discontinuity  (200ms): %d\n", s1.discontinuity200);  // ╮
//   printf("feed-02 book discontinuity  (200ms): %d\n", s2.discontinuity200);  // ╯
//   printf("feed-05 book discontinuity  (200ms): %d\n", s3.discontinuity200);  // ╯

//   printf("feed-01 book discontinuity  (500ms): %d\n", s1.discontinuity500);  // ╮
//   printf("feed-02 book discontinuity  (500ms): %d\n", s2.discontinuity500);  // ╯
//   printf("feed-05 book discontinuity  (500ms): %d\n", s3.discontinuity500);  // ╯

//   printf("feed-01 book discontinuity (2000ms): %d\n", s1.discontinuity2000);  // ╮
//   printf("feed-02 book discontinuity (2000ms): %d\n", s2.discontinuity2000);  // ╯
//   printf("feed-05 book discontinuity (2000ms): %d\n", s3.discontinuity2000);  // ╯

//   printf("feed-01 first time: %ld\n", s1.first);  // ╮
//   printf("feed-02 first time: %ld\n", s2.first);  // ╯
//   printf("feed-02 first time: %ld\n", s3.first);  // ╯
//   printf("feed-01  last time: %ld\n", s1.last);   // ╮
//   printf("feed-02  last time: %ld\n", s2.last);   // ╯
//   printf("feed-05  last time: %ld\n", s3.last);   // ╯
// }

int main(int argc, char* argv[]) {
  // auto s1 = read_coin("feed-01.ap-northeast-2.aws");
  // auto s2 = read_coin("feed-02.ap-northeast-1.aws");
  // auto s3 = read_coin("feed-05.ap-northeast-1.aws");

  // nlohmann::json j;
  // j["s1_book"] = s1.book_ts;
  // j["s1_trade"] = s1.trade_ts;
  // j["s1_ask0p"] = s1.ask0p;

  // j["s2_book"] = s2.book_ts;
  // j["s2_trade"] = s2.trade_ts;
  // j["s2_ask0p"] = s2.ask0p;

  // j["s3_book"] = s3.book_ts;
  // j["s3_trade"] = s3.trade_ts;
  // j["s3_ask0p"] = s3.ask0p;

  // std::string out = j.dump();
  // FILE *fp = fopen("analysis.txt", "w");
  // fprintf(fp, "%s", out.c_str());
  // fclose(fp);

  return 0;
}
